import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args)
	{
		Scanner sc=new Scanner(System.in);
		int n;
		n=sc.nextInt();
		String choice="";
		String str=sc.nextLine();
		String[] num=new String[n];
		for(int i=0;i<n;i++)
		{
			num[i]=sc.nextLine();
		}
		
		while(sc.hasNext())
		{
			choice=sc.nextLine();
			if(choice.equals("sort1"))
				{
					String[] date=new String[n];
					for(int i=0;i<n;i++)
					{
						date[i]=num[i].substring(6, 14);
					}
					Arrays.sort(date);
					for(int i=0;i<n;i++)
						System.out.println(date[i].substring(0, 4)+'-'+date[i].substring(4, 6)+'-'+date[i].substring(6, 8));
				}
			
			else if(choice.equals("sort2"))
			{
				String[] date=new String[n];
				for(int i=0;i<n;i++)
				{
					date[i]=num[i].substring(6, 14);
				}
				Arrays.sort(date);
				for(int i=0;i<n;i++)
				{
					for(int j=0;j<n;j++)
					{
						if(num[j].substring(6,14).equals(date[i]))
						{
							System.out.println(num[j]);
							break;
						}
					}
				}
			}
			else 
			{
				System.out.println("exit");
			    return;
			}
		
		}
		
		
	  }
	}
